/* Copyright (C) 2009 Alvaro Morais <alvaromorais@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details at
 * http://www.gnu.org/copyleft/gpl.html
 */

//  127.0.0.1
// 127.0.0.1:22

// 127.0.0.1:65536
// google.com
// GOOLGE.COM
// 125d

#ifndef BFORCER_H
#define BFORCER_H

#include "bforcerdecls.h"
#include "bforcer_exception.h"

extern "C" {


#include <unistd.h>
#include <getopt.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/limits.h>
}
#include <string>
#include <fstream>
#include <string.h>
#include <stdexcept>
#include <fstream>
#include <list>
#include <libssh/libssh.h>

#ifdef USE_PCRECPP
#include <pcrecpp.h>
#else
#include <boost/regex.hpp>
#endif
#include <boost/filesystem.hpp>   // includes all needed Boost.Filesystem declarations
#include <iostream>

using namespace boost;
using namespace std;

class BForcer
{
public:
    BForcer();
    void setPasswordList(string _filename);
    void setUsername(string _username);
    void setFromArgs(int argc, char** argv);

    void startBruteForce();


    // Allways use Perl expressions
    static bool fullMatch(const string& text,const string& match, bool caseless = true);
    static string fullMatchRetMatch(const string& text, const string& match, bool caseless = true);
    static inline string bforcer_itoa(int num);
    static list<string> retMatchesList(const string& text, const string& match, bool caseless = true);

private:
    string username, filename, host;
    unsigned  sec_to_sleep;

    void printUsage();

    bool tryPassword(string passw);
    bool connect();
    bool disconnect();
    bool reconnect();
    void initOptions();
    bool checkConfigPath();

    void parseHostsFile(string _file);

    list<string> hosts;

    SSH_OPTIONS* opts;
    SSH_SESSION *session;

    string configPath;
    string workingPath;
    string workingHostsFile;
    fstream streamWorkHosts;

    bool force_set;
    bool default_user_set;
    bool usersfile_set;
    bool hosts_file_set;
    bool resume_set;
    bool sleep_set;
    bool char_min_set;
    bool char_max_set;
    bool case_policy_set;

    bool no_curses_set;
    bool port_set;
    bool warning_set;
    bool passwords_files_set;
    bool combination_set;

    void showWarning(const string& warning);
    string default_user;
    string users_file;
    string hosts_file;
    string passwords_file;
    string combinaton;
    bool force;
    bool resume;
    int case_policy;
    int default_port;
    int characters_minimum;
    int characters_maximum;

    void setOptions();

    void NormalizeHosts();
    void InitiateWorkingHostsFile();
    void parseCombination();



    int warning_policy;

    bool addHostToList(string _host);
    void helpCommand(const string& command);

    void lowerString(string& str); // Lower the string





};

#endif // BFORCER_H
